package com.itheima.dubbo.api;

import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.TimeLine;
import com.itheima.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 阿鹏
 * @date 2021/2/2 22:05
 */
@Service
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void addFriend(long userId, long friendId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        if (!mongoTemplate.exists(query, Friend.class)) {
            Friend friend = new Friend();
            friend.setId(ObjectId.get());
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }

        query = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        if (!mongoTemplate.exists(query, Friend.class)) {
            Friend friend = new Friend();
            friend.setId(ObjectId.get());
            friend.setCreated(System.currentTimeMillis());
            friend.setUserId(friendId);
            friend.setFriendId(userId);
            mongoTemplate.save(friend);
        }
    }

    @Override
    public PageResult<Friend> findFriendList(int page, int pagesize, long userId, String keyword) {
        Query query = Query.query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pagesize));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);
        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count, pagesize, pages, page, friendList);
    }

    @Override
    public boolean isFriend(Long userId, Long targetUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("friendId").is(targetUserId));
        return mongoTemplate.exists(query, Friend.class);
    }

    @Override
    public void removeSingleFriend(Long userId, Long targetUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("friendId").is(targetUserId));
        mongoTemplate.remove(query, Friend.class);
    }

    @Override
    public void removeTimeLine(Long friendId, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        mongoTemplate.remove(query, TimeLine.class, "quanzi_time_line_" + friendId);
    }

    @Override
    public List<Friend> findFriendListByUserId(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query, Friend.class);
    }
}
